@file:Suppress("UNUSED_ANONYMOUS_PARAMETER")

package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.NfcA
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.provider.Settings
import android.view.KeyEvent
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.RepairAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.MaintenanceBean
import com.glory.cqaospda.utils.ByteArrayChange
import com.glory.cqaospda.viewmodel.Repair2Model
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import kotlinx.android.synthetic.main.activity_repair.*
import kotlinx.android.synthetic.main.activity_repair2.*
import kotlinx.android.synthetic.main.activity_repair2.rbME
import kotlinx.android.synthetic.main.activity_repair2.rbPE
import kotlinx.android.synthetic.main.activity_title.*
import kotlinx.android.synthetic.main.repair_layout.*

class Repair2Activity: BaseActivity<Repair2Model>(), View.OnClickListener {

    val mMaintenanceList = ArrayList<MaintenanceBean.ItemList>()
    var repairAdapter: RepairAdapter? = null
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@Repair2Activity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {

        viewModel.defUI.callObserve.observe(this@Repair2Activity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        mMaintenanceList.clear()
                        val maintenanceBean = it.item as MaintenanceBean
                        val maintenanceList = ArrayList<MaintenanceBean.ItemList>()
                        for (s in maintenanceBean.mItemList) {
                            if ("0" == s.mPriority) {
                                maintenanceList.add(s)
                            }
                        }
                        for (s in maintenanceBean.mItemList) {
                            if ("1" == s.mPriority) {
                                maintenanceList.add(s)
                            }
                        }
                        for (s in maintenanceBean.mItemList) {
                            if ("2" == s.mPriority) {
                                maintenanceList.add(s)
                            }
                        }
                        mMaintenanceList.addAll(maintenanceList)

                        repairAdapter!!.setList(maintenanceList)
                    }
                    2 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("接单成功", true)
                        viewModel.defaultGetBaseConfig()
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text= "维修数据"
        tvUserName.text = Constant.UserName
        tvBaseSubmit.visibility = View.GONE
        tvLongTime.visibility = View.VISIBLE
        initRecyclerView(rvRepairList, this)
        repairAdapter = RepairAdapter(null)
        rvRepairList.adapter = repairAdapter
        val instance = MyApp.instance
        instance!!.handler = handler
        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        viewModel.defaultGetBaseConfig()

        rbPE.setOnClickListener(this)
        rbME.setOnClickListener(this)

        repairAdapter!!.setOnItemClickListener{ itemList,position ->
            if (Constant.isFastClick()) {
                if (itemList.mMaintainType != "0") {
                    if (Constant.UserName != itemList.mUserName) {
                        viewModel.defUI.toastEvent.value =
                            ToastMessageBean("当前操作不允许", false)
                        return@setOnItemClickListener
                    }
                }
                when (itemList.mMaintainType) {
                    "0" -> {
                        val map = JsonObject()
                        map.addProperty("UserName", Constant.UserName)
                        map.addProperty("OrderID", itemList.mOrderID)
                        map.addProperty("MaintainType", "0")
                        map.addProperty("EquipmentName", "")
                        map.addProperty("LotNo", "")
                        map.addProperty("MaintainContent", "")
                        map.addProperty("MaintainVerification", "")
                        map.addProperty("MaintainResults", "")
                        map.addProperty("Comment", "")
                        map.addProperty("PartsList", "")
                        map.addProperty("PartsID", "")
                        viewModel.defaultMaintenanceOrder(map)
                    }
                    "1" -> {
                        intent.putExtra("itemList", itemList)
                        intent.setClass(this, Repair2ItemActivity::class.java)
                        startActivity(intent)
                    }
                }
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_repair2
    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onClick(view: View?) {
        when (view!!.id) {
            R.id.rbPE ->{
                val maintenanceList = ArrayList<MaintenanceBean.ItemList>()
                mMaintenanceList.forEach{
                    if (it.mGroupName == "PE"){
                        maintenanceList.add(it)
                    }
                }
                repairAdapter!!.setList(maintenanceList)
            }
            R.id.rbME ->{
                val maintenanceList = ArrayList<MaintenanceBean.ItemList>()
                mMaintenanceList.forEach{
                    if (it.mGroupName == "ME"){
                        maintenanceList.add(it)
                    }
                }
                repairAdapter!!.setList(maintenanceList)
            }
        }
    }
}